package cn.cmcc.online.smsapi.ui.widget;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import cn.cmcc.online.smsapi.ui.R;
import cn.cmcc.online.smsapi.ui.model.TransportSelectInfo;

public class TransportSelectDialog extends Dialog {
    private Context mContext = null;
    private TransportSelectInfo mTransportSelectInfo = null;
    private int mCurrentIndex = 0;
    private int mLastIndex = 0;
    private List<View> mViewList = new ArrayList<>();

    private View mCustomView;
    private TextView mTitle;
    private RelativeLayout mOKBtn;
    private RelativeLayout mCancleBtn;

    public TransportSelectDialog(Context context, TransportSelectInfo selectInfo, int index) {
        super(context);
        mContext = context;
        mTransportSelectInfo = selectInfo;
        mCurrentIndex = index;
        mLastIndex = index;
        initWindow();
    }

    public int getSelected() {
        return mCurrentIndex;
    }

    private void initWindow() {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        Window window = getWindow();
        window.setBackgroundDrawableResource(R.drawable.bubble_select_dialog_bg);
        window.setGravity(Gravity.BOTTOM);
        window.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(lp);

        initView();
        initData();
    }

    private void initView() {
        mCustomView = LayoutInflater.from(mContext).inflate(R.layout.bubble_select_list_dialog, null);
        mTitle = (TextView) mCustomView.findViewById(R.id.title);
        mOKBtn = (RelativeLayout) mCustomView.findViewById(R.id.dialog_btn_ok_layout);
        mCancleBtn = (RelativeLayout) mCustomView.findViewById(R.id.dialog_btn_cancel_layout);

        mOKBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        mCancleBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCurrentIndex = mLastIndex;
                dismiss();
            }
        });

        setContentView(mCustomView);
    }

    private void initData() {
        mTitle.setText(mTransportSelectInfo.getTitle());

        LinearLayout rootLayout = (LinearLayout) mCustomView.findViewById(R.id.item_roots);
        int index = 0;
        for (TransportSelectInfo.TransportInfo info : mTransportSelectInfo.getInfoList()) {
            View childView = View.inflate(mContext, R.layout.bubble_list_items_content_part, null);
            TextView subject = (TextView) childView.findViewById(R.id.item_text);
            TextView description = (TextView) childView.findViewById(R.id.item_info);
            View splitLine = childView.findViewById(R.id.bubble_bottom_split_line);
            if (info.getDescription() == null || "".equalsIgnoreCase(info.getDescription()) || "null".equalsIgnoreCase(info.getDescription())) {
                description.setVisibility(View.GONE);
            } else {
                description.setVisibility(View.VISIBLE);
                description.setText(info.getDescription());
            }
            subject.setText(info.getSubject());
            childView.setTag(index++);
            childView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mLastIndex = mCurrentIndex;
                    mCurrentIndex = (int) v.getTag();
                    checkItem();
                }
            });

            if (index == mTransportSelectInfo.getInfoList().size()) {
                splitLine.setVisibility(View.GONE);
            } else {
                splitLine.setVisibility(View.VISIBLE);
            }

            mViewList.add(childView);
            rootLayout.addView(childView);
        }

        checkItem();
    }

    private void checkItem() {
        for (int i = 0; i < mViewList.size(); i++) {
            View check = mViewList.get(i).findViewById(R.id.item_check);
            TextView item_title = (TextView) mViewList.get(i).findViewById(R.id.item_text);
            TextView item_info = (TextView) mViewList.get(i).findViewById(R.id.item_info);
            if (i == mCurrentIndex) {
                check.setVisibility(View.VISIBLE);
                check.setBackgroundResource(R.drawable.btn_radio_off_pressed_holo_light);
                item_title.setTextColor(mContext.getResources().getColor(R.color.bubble_dialog_item_selected));
                item_info.setTextColor(mContext.getResources().getColor(R.color.bubble_dialog_item_selected));
            } else {
                check.setVisibility(View.INVISIBLE);
                item_title.setTextColor(mContext.getResources().getColor(R.color.bubble_dialog_item_unselected));
                item_info.setTextColor(mContext.getResources().getColor(R.color.bubble_dialog_item_unselected));
            }
        }
    }
}
